package com.gem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.entity.Cart;
import com.gem.entity.User;
import com.gem.entity.UserAddress;
import com.gem.service.CartService;
import com.gem.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 刘聪
 * Created  on 2021/4/2
 */
@Controller
@RequestMapping("/cart")

public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private UserAddressService addressService;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(@PathVariable("productId") Integer productId,
                      @PathVariable("price") Double price,
                      @PathVariable("quantity") Integer quantity,
                      HttpSession session) {
        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setCost(price * quantity);
        User user = (User) session.getAttribute("User");
        cart.setUserId(user.getId());
        cartService.save(cart);
        return "redirect:/cart/findAllCart";
    }

    @PostMapping("/update/{id}/{quantity}/{cost}")
    @ResponseBody
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Double cost
    ){
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);
        if(cartService.updateById(cart)){
            return "success";
        }else{
            return "fail";
        }
    }

    @GetMapping("/findAllCart")
    public String findAllCart(Model model, HttpSession session) {

        User user = (User) session.getAttribute("User");
        model.addAttribute("cartList", cartService.findAllCartVOByUserId(user.getId()));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", user.getId());
        List<UserAddress> addressList = (List<UserAddress>) addressService.list(wrapper);
        model.addAttribute("addressList", addressList);
        return "cart/findAllCart";
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        cartService.removeById(id);
        return "redirect:/cart/findAllCart";
    }

    @GetMapping("/deleteAddress/{id}")
    public String deleteAddress(@PathVariable("id") Integer id) {
        addressService.removeById(id);
        return "redirect:/cart/findAllCart";
    }

    @PostMapping("/address")
    public String address(UserAddress userAddress) {
        addressService.save(userAddress);
        return "redirect:/cart/findAllCart";
    }

    @GetMapping("/defaultAddress/{id}")
    public String defaultAddress(@PathVariable("id") Integer id,HttpSession session) {
        User user = (User) session.getAttribute("User");
        List<UserAddress> addressList = addressService.dafultAddress(user.getId(), id);
//        addressList.forEach(System.out::println);
        addressService.updateBatchById(addressList);

//        批处理-rewriteBatchedStatements=true
        return "redirect:/cart/findAllCart";
    }
}
